package cn.itsource.zcn.searchmastermessage.controller;

import cn.itsource.zcn.searchmastermessage.domain.SearchMasterMessage;
import cn.itsource.zcn.searchmastermessage.query.SearchMasterMessageQuery;
import cn.itsource.zcn.searchmastermessage.service.ISearchMasterMessageService;
import cn.itsource.zcn.utils.AjaxResult;
import cn.itsource.zcn.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * (SearchMasterMessage)表控制层
 *
 * @author makejava
 * @since 2021-08-22 16:58:49
 */
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMessageController {
    /**
     * 服务对象
     */
    @Autowired
    private ISearchMasterMessageService tSearchMasterMessageService;

    /**
     * 查询全部数据
     *
     * @return List<>
     */
    @RequestMapping("/all")
    public List<SearchMasterMessage> queryAll() {
        return tSearchMasterMessageService.queryAll();
    }

    /**
     * 寻主信息发布
     * @param searchMasterMessage
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMessage searchMasterMessage, HttpServletRequest request){
        return tSearchMasterMessageService.publish(searchMasterMessage,request);
    }

    /**
     * 添加或者修改
     * @return AjaxResult
     */
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody SearchMasterMessage searchMasterMessage) {
        try{
            if(searchMasterMessage.getId()!=null){
                tSearchMasterMessageService.update(searchMasterMessage);
            }else{
                tSearchMasterMessageService.save(searchMasterMessage);
            }
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }
    /**
     * 删除数据
     * @return AjaxResult
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            tSearchMasterMessageService.delete(id);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    /**
     * 查询一条数据
     * @return List<>
     */
    @GetMapping("/{id}")
    public SearchMasterMessage queryById(@PathVariable("id") Long id) {
        return tSearchMasterMessageService.queryById(id);
    }


    /**
     * 查询高级+分页数据
     * @return QueryResult
     */
    @PostMapping
    public QueryResult queryPage(@RequestBody SearchMasterMessageQuery searchMasterMessageQuery, HttpServletRequest request) {
//        System.out.println(shopQuery);
        return tSearchMasterMessageService.queryPage(searchMasterMessageQuery,request);
    }
    /**
     * 批量删除
     * @return AjaxResult
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody  Long[] ids) {
        try {
            tSearchMasterMessageService.deleteBatch(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }

    }


}
